package com.office.automate.email.framework;

import java.util.Properties;

import javax.mail.PasswordAuthentication;
import javax.mail.Session;

import com.office.automate.configuration.property.ConfigurationProperties;

public class EmailConfigurations {
	private static String MAIL_SMTP_HOST = "mail.smtp.host";
	private static String MAIL_SMTP_SOCKETFACTORY_PORT = "mail.smtp.socketFactory.port";
	private static String MAIL_SMTP_SOCKETFACTORY_CLASS = "mail.smtp.socketFactory.class";
	private static String MAIL_SMTP_AUTH = "mail.smtp.auth";
	private static String MAIL_SMTP_PORT = "mail.smtp.port";

	private static Properties props = new Properties();
	private static Session session = null;

	static {
    	try {
    		final Properties configProps = ConfigurationProperties.getProps();
			props.put(MAIL_SMTP_HOST, configProps.getProperty("MAIL_SMTP_HOST"));
			props.put(MAIL_SMTP_SOCKETFACTORY_PORT,
					configProps.getProperty("MAIL_SMTP_SOCKETFACTORY_PORT"));
			props.put(MAIL_SMTP_SOCKETFACTORY_CLASS,
					configProps.getProperty("MAIL_SMTP_SOCKETFACTORY_CLASS"));
			props.put(MAIL_SMTP_AUTH, configProps.getProperty("MAIL_SMTP_AUTH"));
			props.put(MAIL_SMTP_PORT, configProps.getProperty("MAIL_SMTP_PORT"));

		    session = Session.getDefaultInstance(props,
				   new javax.mail.Authenticator() {
					   protected PasswordAuthentication getPasswordAuthentication() {
						   return new PasswordAuthentication(configProps.getProperty("MAIL_ACCOUNT"),
								   							 configProps.getProperty("MAIL_ACCOUNT_PASSWORD"));
						   }
					  });
	 	} catch (Exception ex) {
	 		ex.printStackTrace();
	    }
	}

	public static Session getSession() {
		return session;
	}
}
